/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.PolylineDecoration;
import org.eclipse.draw2d.RotatableDecoration;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;

import edu.pku.sei.modeler.handler.MiddleEndpointLocator;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.uml.Kernel.PackageImport;
import edu.pku.sei.modeler.model.uml.Kernel.VisibilityKind;

/**
 * @generated
 * 
 */
public class PackageImportEditpart extends UMLLinkEditpart {
	private RotatableDecoration targetDecor;

	/**
	 * The predefined keywords are <<import>> for a public package import, and <<access>>
	 * for a private package import.
	 */
	private Label nameLabel;

	public PackageImportEditpart(MetaShapeElement model) {
		super(model);
	}

	protected void createEditPolicies() {
		super.createEditPolicies();
	}

	private PolylineConnection getConnection() {
		return (PolylineConnection) getFigure();
	}

	protected IFigure createFigure() {
		PolylineConnection conn = new PolylineConnection();
		conn.setLineStyle(2);
		nameLabel = new Label("<<" + "import" + ">>");
		FontData[] fontdata = { new FontData("Arial", 10, SWT.BOLD) };
		nameLabel.setFont(new Font(Display.getCurrent(), fontdata));
		MiddleEndpointLocator relationshipLocator = new MiddleEndpointLocator(
				conn, new Point(0, 0));

		conn.add(nameLabel, relationshipLocator);
		return conn;
	}

	private PolylineDecoration createDecoration() {
		PolylineDecoration decoration = new PolylineDecoration();
		decoration.setScale(10, 5);
		return decoration;
	}

	protected void handlePropertyChanged(PropertyChangeEvent event) {

	}

	protected void refreshVisuals() {
	    PackageImport packageImport = (PackageImport) this.getLink().getModelElement();
	    if (packageImport.getVisibility().equals(VisibilityKind.PRIVATE)) {
	    	nameLabel.setText("<<" + "access" + ">>");
	    }else {
	    	nameLabel.setText("<<" + "import" + ">>");
	    }

		updateTargetDecoration();

		super.refreshVisuals();
	}

	private void updateTargetDecoration() {
		if (targetDecor == null) {
			targetDecor = createDecoration();
			getConnection().setTargetDecoration(targetDecor);
		}
	}
}
